package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.ContractorAudit;
import com.weitu.contractor.entity.ContractorAuditCriteria;
import com.weitu.contractor.mapper.ContractorAuditMapper;
import com.weitu.contractor.service.ContractorAuditService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContractorAuditServiceImpl implements ContractorAuditService {
    private final ContractorAuditMapper contractorAuditMapper;

    private static final Logger logger = LoggerFactory.getLogger(ContractorAuditServiceImpl.class);

    @Autowired
    public ContractorAuditServiceImpl(ContractorAuditMapper contractorAuditMapper) {
        this.contractorAuditMapper = contractorAuditMapper;
    }

    @Override
    public int countByCriteria(ContractorAuditCriteria example) {
        int count = this.contractorAuditMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public ContractorAudit selectByPrimaryKey(String id) {
        return this.contractorAuditMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractorAudit> selectByCriteria(ContractorAuditCriteria example) {
        return this.contractorAuditMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.contractorAuditMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(ContractorAudit record) {
        return this.contractorAuditMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(ContractorAudit record, ContractorAuditCriteria example) {
        return this.contractorAuditMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(ContractorAudit record) {
        return this.contractorAuditMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfExist(String contractorId, String contractorTypeId) {
        ContractorAuditCriteria criteria = new ContractorAuditCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorIdEqualTo(contractorId)
                .andContractorTypeIdEqualTo(contractorTypeId);
        return contractorAuditMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public boolean checkIfExist(String contractorId, String contractorTypeId, String id) {
        ContractorAuditCriteria criteria = new ContractorAuditCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorIdEqualTo(contractorId)
                .andContractorTypeIdEqualTo(contractorTypeId)
                .andIdNotEqualTo(id);
        return contractorAuditMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public List<String> selectContractorAuditIdsByName(String name) {
        return this.contractorAuditMapper.selectContractorAuditIdsByName(name);
    }
}